package com.uestc.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Json操作工具类
 * 涉及的包有：commons-beanuitls-1.7.0.jar、commons-collections-3.2.jar、commons-lang-2.
 * 4.jar、commons-logging-1.2.jar、ezmorph-1.0.4.jar、json-lib-2.4-jdk15.jar
 * 
 * @author justyoung
 *
 */
public class JsonUtil {

	private JSONObject jsonObject;
	private JSONArray jsonArray;
	public static final int JSONOBJECT = 1;
	public static final int JSONARRAY = 2;

	public JsonUtil(String jsonString, int option) {
		if (option == JSONOBJECT) {
			this.jsonObject = JSONObject.fromObject(jsonString);
			this.jsonArray = null;
		} else if (option == JSONARRAY) {
			this.jsonArray = JSONArray.fromObject(jsonString);
			this.jsonObject = null;
		}
	}

	/**
	 * 实例化的方式，来获得JSON对象的某个key对应的value
	 * 
	 * @param propertyName
	 * @return
	 */
	public String getAttribute(String propertyName) {
		try {
			String value = this.jsonObject.get(propertyName).toString();
			return value;
		} catch (Exception e) {
			return null;
		} 
		
	}

	/**
	 * 返回JSON数组的index下标对应的字符串
	 * 
	 * @param index
	 * @return
	 */
	public String getValue(int index) {
		return this.jsonArray.get(index).toString();
	}

	/**
	 * 根据JSON字符串来获取某个key对应的value值
	 * 
	 * @param jsonString
	 * @param propertyName
	 * @return
	 */
	public static String getAttribute(String jsonString, String propertyName) {
		JSONObject jsonObject = JSONObject.fromObject(jsonString);
		return jsonObject.get(propertyName).toString();
	}

	/**
	 * 返回JSON字符串各元素组成的数组
	 * 
	 * @param jsonString
	 * @param propertyName
	 * @return
	 */
	public static String[] getArray(String jsonString) throws NullPointerException {
		JSONArray jsonArray = JSONArray.fromObject(jsonString);
		Object[] objects = jsonArray.toArray();
		if (objects == null) {
			throw new NullPointerException("jsonArray is null");
		}
		String objectString[] = new String[objects.length];
		for (int i = 0; i < objects.length; ++i) {
			objectString[i] = String.valueOf(objects[i]);

		}
		return objectString;
	}

	/**
	 * 创建JSON字符串，key-value形式
	 * 
	 * @param map
	 * @return
	 */
	public static String createJsonString(Map<String, ?> map) {
		JSONObject jsonObject = new JSONObject();
		for (Entry<String, ?> item : map.entrySet()) {
			jsonObject.put(item.getKey(), item.getValue());
		}
		return jsonObject.toString();
	}

	/**
	 * 多态：返回一个JSONArray的字符串，数组形式
	 * 
	 * @param list
	 * @return
	 */
	public static String createJsonString(List<Object> list) {
		JSONArray jsonArray = new JSONArray();
		for (Object item : list) {
			jsonArray.add(item);
		}
		return jsonArray.toString();
	}

	@Override
	public String toString() {
		if (jsonArray != null)
			return jsonArray.toString();
		else
			return jsonObject.toString();
	}

	/**
	 * 返回这个json组成的hashmap, 目前只针对JSONObject
	 * 
	 * @return
	 */
	public HashMap<String, String> toHashMap() {
		if (jsonObject == null)
			return null;
		HashMap<String, String> map = new HashMap<>();
		@SuppressWarnings("unchecked")
		Set<String> set = jsonObject.keySet();
		for (String key : set) {
			map.put(key, jsonObject.getString(key));
		}
		return map;
	}

	/**
	 * 目前只针对jsonObject，删除jsonObject中value为空的字符串, 必须是String，key和value
	 */
	@SuppressWarnings("unchecked")
	public void trim() {
		if (jsonObject == null)
			return;
		ArrayList<String> keys = new ArrayList<>();
		Iterator<String> iterator = jsonObject.keys();
		while (iterator.hasNext()) {
			String key = (String) iterator.next();
			String value = (String) jsonObject.get(key);
			if (value == null || "".equals(value)) {
				keys.add(key);
			}
		}
		for (String key : keys) {
			jsonObject.remove(key);
		}
	}

	public static ArrayList<Object> getMapList(String json) {
		ArrayList<Object> arrayList = new ArrayList<Object>();
		JSONArray jsonArray = JSONArray.fromObject(json);
		for (Object object : jsonArray) {
			arrayList.add(JSONObject.toBean(JSONObject.fromObject(object), Map.class));
		}
		return arrayList;
	}

	public static void main(String[] args) {
		ArrayList<Integer> arrayList = new ArrayList<>();
		arrayList.add(2);
		arrayList.add(2);
		arrayList.add(2);
		arrayList.add(2);
		arrayList.add(2);
		arrayList.add(3);
		arrayList.add(2);
		arrayList.add(2);
		arrayList.add(2);
		arrayList.add(2);
		for (int i : arrayList) {
			if (i == 3)
				arrayList.remove(i);
		}
	}

}
